package org.dicr.util.reflect;

import java.lang.reflect.*;

/**
 * Data dumper for debugging purposes
 * 
 * @author Igor A Tarasov <develop@dicr.org>
 */
public class Dumper {

	/**
	 * Dump values of object properties
	 * 
	 * @param obj object to dump
	 * @return content of object
	 */
	public static String dump(Object obj) {
		if (obj == null) return null;
		StringBuilder sb = new StringBuilder();
        sb.append(obj.getClass().getSimpleName()).append('{');
		Field[] fields = obj.getClass().getDeclaredFields();
        boolean firstRound = true;
        for (Field field : fields) try {
            field.setAccessible(true);
        	final Object objVal = field.get(obj);
        	final String value = objVal != null ? objVal.toString() : "null";
            if (!firstRound) sb.append(",");
	        firstRound = false;
            sb.append(field.getName()).append('=').append('\'') .append(value).append('\'');
        } catch (IllegalAccessException ignore) {
        	//this should never happen
        }
        sb.append('}');
        return sb.toString();
	}
}
